package likouJava;

import java.util.ArrayList;
import java.util.List;

public class 二叉树展开为链表 {
    public void flatten(TreeNode root) {
        if (root == null) return;

        //递归展开左右子树
        flatten(root.left);
        flatten(root.right);

        //保存右指数
        TreeNode rightSubTree = root.right;

        //把左子树移到右边，将左边设为空
        root.right = root.left;
        root.left = null;

       //找到现在右子树的尾部
        TreeNode p = root;
        while (p.right != null){
            p = p.right;
        }

        //将原来的右子树拼接到后面
        p.right = rightSubTree;
    }



}
